package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.MyConsult;
import com.hs.service.MyConsultService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("myconsult")
public class MyConsultControler {
    @Autowired
    private MyConsultService myConsultService;
    @PostMapping
    public R addmyConsult(@RequestBody MyConsult myConsult) {
        int i = myConsultService.addMyConsult(myConsult);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @DeleteMapping("{id}")
    public R deleteMyConsult(@PathVariable("id") int id) {
        int i = myConsultService.deleteMyConsult(id);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @PutMapping
    public R updateMyConsult(@RequestBody MyConsult myConsult) {
        int i = myConsultService.updateMyConsult(myConsult);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @GetMapping("{id}")
    public R getMyConsultById(@PathVariable("id") int id) {
        MyConsult myConsult = myConsultService.getMyConsultById(id);
        return R.ok(myConsult);
    }
    @GetMapping("page")
    public R getMyConsultByPage(MyConsult myConsult,
                                @RequestParam(required = false,defaultValue = "1") int pageNum,
                                @RequestParam(required = false,defaultValue = "2") int pageSize) {
        PageInfo<MyConsult> MyConsultByPage = myConsultService.getMyConsultByPage(myConsult, pageNum, pageSize);
        return R.ok(MyConsultByPage);
    }
    @GetMapping("userId")
    public R getMyConsultByUserId( Integer userId,@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false,defaultValue = "3") Integer pageSize) {
        PageInfo<MyConsult> myConsultByUserId = myConsultService.getMyConsultByUserId(userId, pageNum, pageSize);
        return R.ok(myConsultByUserId);
    }
    @GetMapping("/myConsult/{userId}")
    public R getMyConsult(@PathVariable Integer userId) {
        // 查询已完成和未完成的咨询记录
        List<MyConsult> finished = myConsultService.getFinishedConsults(userId);
        List<MyConsult> unfinished = myConsultService.getUnfinishedConsults(userId);

        // 封装结果
        Map<String, Object> data = new HashMap<>();
        data.put("finished", finished);
        data.put("unfinished", unfinished);

        return R.ok(data);
    }
    @PutMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id) {
        boolean success = myConsultService.updateConsultStatus(id);
        if (success) {
            return R.ok("状态更新成功");
        } else {
            return R.error("更新失败，记录不存在");
        }
    }
    @PutMapping("/updateAss/{consultantId}")
    public R updateAss(@PathVariable Integer consultantId){
        int i = myConsultService.updateMyConsultByIds(consultantId);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("");
        }
    }
}
